package com.example.footballsystem.service;

import com.example.footballsystem.entity.User;
import com.example.footballsystem.entity.userDairy;
import com.example.footballsystem.mapper.UserAccountMapper;
import com.example.footballsystem.mapper.UserDairyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class UserService {
    @Autowired
    UserAccountMapper userAccountMapper;
    @Autowired
    UserDairyMapper userDairyMapper;
    //登录
    public boolean Logincheck(String account,String password,String ip,String Os,String Ie){
        if(userAccountMapper.getPassword(account)==null){
            return false;
        }
        userDairy dairy= new userDairy();
        Timestamp time = new Timestamp(System.currentTimeMillis());
        dairy.setUser(userAccountMapper.getUserByAccount(account));
        dairy.setIp(ip);
        dairy.setIe(Ie);
        dairy.setOs(Os);
        dairy.setLogintime(time);
        userDairyMapper.addUserDairy(dairy);
        return password.equals(userAccountMapper.getPassword(account));
    }

    //注册
    public boolean regist(User user){
        if(userAccountMapper.getUserByAccount(user.getAccount())==null){
            userAccountMapper.addUser(user);
            Timestamp time = new Timestamp(System.currentTimeMillis());
            userAccountMapper.setCreateTime(time, user.getAccount());
            return true;
        }else {
            return false;
        }
    }

    //查询用户
    public List<User> search(String info){
        List<User> resultList = new ArrayList<>();
        int count = 0;
        if(userAccountMapper.getUserByAccount(info)!=null){
            resultList.add(userAccountMapper.getUserByAccount(info));
            count++;
        }
        if(userAccountMapper.getUserByPhone(info)!=null){
                resultList.add(userAccountMapper.getUserByPhone(info));
                count++;
        }

        if(userAccountMapper.getUserByEmail(info)!=null){
                resultList.add(userAccountMapper.getUserByEmail(info));
                count++;
        }

        if(userAccountMapper.getUserByUserName(info)!=null){
                resultList.add(userAccountMapper.getUserByUserName(info));
                count++;
        }
        log.info(resultList.toString());
        for (int i=0;i<count;i++) {
            for (int j=i+1;j<count;j++) {
                if(Objects.equals(resultList.get(j).getAccount(), resultList.get(j).getAccount())){
                    log.info(i+" "+j);
                    resultList.remove(j);
                    j--;
                    count--;
                }
            }
        }
        //log.info(resultList.toString());
        return resultList;
    }
    //获取所有用户信息
    public List<User> getAllUser(){
        return userAccountMapper.getAllUser();
    }
    //
    public User getUser(String account){
        return userAccountMapper.getUserByAccount(account);
    }
    public Boolean changeUserName(String account,String username){
        return userAccountMapper.updateUserName(account, username);
    }
    public Boolean changeUserSex(String account,String sex){
        return userAccountMapper.updateUserSex(account, sex);
    }
    public Boolean changeUserPassword(String account,String password){
        return userAccountMapper.updateUserPassword(account, password);
    }
    public Boolean changeUserEmail(String account,String email){
        return userAccountMapper.updateUserEmail(account, email);
    }
    public Boolean changeUserBirthday(String account,String birthday){
        //Timestamp day = new Timestamp(birthday);//String str = "2018-05-06 10:30:40";
        return userAccountMapper.updateUserBirthday(account, birthday);
    }
    public Boolean changeUrl(String account,String url){
        return userAccountMapper.updateUserUrl(account, url);
    }
    public Boolean deleteUser(String account){
        return userAccountMapper.deleteUser(account);
    }
    public Boolean addScore(String account,int score){
        return userAccountMapper.setScore(account,score);
    }
    public List<Integer> getUserCount(){
        return userAccountMapper.countLoginTime();
    }
    public List<User> getRank(){
        List<User> resultList = new ArrayList<>();
        resultList=userAccountMapper.getAllUser();
        Comparator<User> userComparator = Comparator.comparing(User::getScore);
        resultList.sort(userComparator);
        return resultList;
    }
    public Integer count(){
        return userDairyMapper.count();
    }
}
